/**
 * Created by Administrator on 2016/6/2 0002.
 */
var AppService = {
  urlroot: "http://euvee.vicp.co/rest/member/",
  init: function () {
    Vue.http.options.root = this.urlroot;
  },
  //初始化引入数据
  find: function (inputQuery, pagenum, pagesteps, items, success_callback, error_callback) {
    var url = this.urlroot;
    Vue.http.get(url, function (data, status, request) {
      var i = data.length;
      success_callback(data, status, request);
    }).catch(function (data, status, request) {
      if (error_callback != null) {
        error_callback(data, status, request);
      }
    });
  },
  findById: function (inputQuery, success_callback, error_callback) {
    var url = this.urlroot;
    Vue.http.get(url + inputQuery, function (data, status, request) {
      success_callback(data, status, request);
    }).catch(function (data, status, request) {
      if (error_callback != null) {
        error_callback(data, status, request);
      }
    });
  },
  add: function (items, success_callback, error_callback) {
    Vue.http.post(this.urlroot, items, function (data, status, request) {
      success_callback(data, status, request);
    }).catch(function (data, status, request) {
      if (error_callback != null) {
        error_callback(data, status, request);
      }
    });
  },
  //绑定id，删除数据
  remove: function (item, success_callback, error_callback) {
    Vue.http.delete(this.urlroot + item.id, function (data, status, request) {
      success_callback(data, status, request);
    }).catch(function (data, status, request) {
      if (error_callback != null) {
        error_callback(data, status, request);
      }
    })
  },
//修改节点信息
  edit: function (item, success_callback, error_callback) {
    Vue.http.put(this.urlroot, item, function (data, status, request) {
      success_callback(data, status, request);
    }).catch(function (data, status, request) {
        if (error_callback != null) {
          error_callback(data, status, request);
        }
      }
    )
  },
  //显示详情
  details: function (item, success_callback, error_callback) {
    Vue.http.get(this.urlroot + item.uuid, function (data, status, request) {
      success_callback(data, status, request);
    }).catch(function (data, status, request) {
        if (error_callback != null) {
          error_callback(data, status, request);
        }
      }
    )
  },
  //返回父级目录
  backToParent: function (p, success_callback, error_callback) {
    Vue.http.get(this.urlroot + p + "/children", function (data, status, request) {
      success_callback(data, status, request);
    }).catch(function (data, status, request) {
        if (error_callback != null) {
          error_callback(data, status, request);
        }
      }
    );
  },
  backToParent2: function (parentid, success_callback, error_callback) {
    Vue.http.get(this.urlroot + parentid, function (data, status, request) {
      success_callback(data, status, request);
    }).catch(function (data, status, request) {
        if (error_callback != null) {
          error_callback(data, status, request);
        }
      }
    );
  },
//切换节点
  change: function (item, success_callback, error_callback) {
    Vue.http.get(this.urlroot + item.uuid + "/children", function (data, status, request) {
      success_callback(data, status, request);
    }).catch(function (data, status, request) {
        if (error_callback != null) {
          error_callback(data, status, request);
        }
      }
    );
  }
};
var svc = Object.create(AppService);

Vue.component('itemdetails', {
  template: '#itemdetails-tpl',
  mode: "modeItemView",
  //可以使用 props 把数据传给子组件。
  // “prop” 是组件数据的一个字段，期望从父组件传下来。子组件需要显式地用 props 选项 声明 props：
  props: {
    item: {
      type: Object,
      twoWay: false,
      required: true
    },
    mode: {
      type: String,
      default: 'modeList'
    },
    onCancel: {
      type: Function,
      required: true
    }
  },

  data: function () {
    return {
      result: null,
    }
  },

  methods: {
    onCancelClick: function (e) {
      console.log("onCancelClick: inside component itemeditor");
      vm.onCancel();
    }
  }
});// ------end Vue.component('itemdetails')

Vue.component('itemeditor', {
  template: '#itemeditor-tpl',
  mode: "modeEdit",
  //可以使用 props 把数据传给子组件。
  // “prop” 是组件数据的一个字段，期望从父组件传下来。子组件需要显式地用 props 选项 声明 props：
  props: {
    item: {
      type: Object,
      twoWay: false,
      required: true
    },

    mode: {
      type: String,
      default: 'modeList'
    },
    onSubmit: {
      type: Function,
      required: true
    },
    onCancel: {
      type: Function,
      required: true
    }
  },

  data: function () {
    return {
      result: null,
    }
  },

  methods: {
    onSubmitClick: function (e) {
      console.log("onSubmitClick: inside component itemeditor");
      vm.onSave(this.mode, this.item);
    },
    onCancelClick: function (e) {
      console.log("onCancelClick: inside component itemeditor");
      vm.onCancel();
    }
  }
});// ------end Vue.component('itemeditor')

Vue.component('itemlist', {
  template: '#itemlist-tpl',
  mode: "modeList",

  props: {
    items: {
      type: Object,
      twoWay: false,
      required: true
    },
    mode: {
      type: String,
      default: 'modeList'
    },
    onRemove: {
      type: Function,
      required: true
    }
  },
  components: {},
  data: function () {
    return {
      result: null,
    }
  },
  methods: {
    onEdit: function (item, mode) {
      console.log("onEditClick: inside component itemelist");
      vm.curitem = item;
      vm.setMode(mode);
    },
    onDetails: function (item, mode) {
      console.log("onDetailsClick: inside component itemelist");
      vm.curitem = item;
      vm.setMode(mode);
    },
    onRemoveItem: function (item, e) {
      console.log("onRemoveClick: inside component itemelist");
      vm.onRemoveItem(item, this.mode);
    },
    onChangeItem: function (item) {
      console.log("onChangeItem: inside component itemelist");
      vm.onChange(item);
    }
  },
  ready: function () {
  }
});
/*--/itemlist--*/

Vue.component('itemsearch', {
  template: '#itemsearch-tpl',
  mode: "modeSearch",
  props: {
    item: {
      type: Object,
      twoWay: false,
      required: true
    },
    onRemove: {
      type: Function,
      required: true
    }
  },
  data: function () {
    return {
      result: null
    }
  },
  methods: {
    onAdd: function () {
      console.log("setMode: inside component itemsearch");
      vm.setMode("modeAddNew");
    },
    onRemoveSelected: function () {
      var items = vm.items;
      var r = confirm("确定删除选中人员？");
      if (r == true) {
        for (var i in items) {
          if (items[i].selected) {
            svc.remove(items[i], function (data, status, request) {
              vm._find('', 1, 6);
            })
          }
        }
      }
    }
  }
});// ------end Vue.component('itemsearch')

var vm = new Vue({

  el: '#res',
  data: {
    currentView: 'itemeditor',
    begtime: '',
    endtime: '',
    items: [],
    items2: [],
    curitem: {},
    mode: 'modeList',
    modeTitle: "",
    user: '',
    searchQuery: '',
    uuid: '',
    uploadedFiles: [], // my list for the v-for
    fileProgress: 0, // global progress
    allFilesUploaded: false, // is everything done?
    curdate:""
  },
  compinents: {},

  methods: {
    _find: function (inputQuery) {
      var self = this;
      svc.findById(inputQuery, function (data, status, request) {
        self.$set('items', data);
        if (self.items.length > 0) {
          self.$set("curitem", self.items[0]);
        }
      });
    },
    onRemove: function (item) {
      var self = this;
      var r = confirm("确定删除？");
      if (r == true) {
        svc.remove(item, function (data, status, request) {
            self.$set('curitem', item);
            self._find('', 1, 6);
          }
        )
      }
    },
    //显示全部信息
    basicsearch: function (e) {
      console.log(this.searchQuery);
      this._find(this.searchQuery, 1, 5);
    },
    onCancel: function () {
      console.log("onCancel");
      this.curitem = {};
      this.setMode('modeList');
    },
    init: function () {
      svc.init();
      var datetime = new Date();
      datetime.getYear();        //获取当前年份(2位)
      datetime.getFullYear();    //获取完整的年份(4位,1970-????)
      datetime.getMonth();       //获取当前月份(0-11,0代表1月)
      datetime.getDate();        //获取当前日(1-31)
      datetime.getDay();         //获取当前星期X(0-6,0代表星期天)
      datetime.getTime();        //获取当前时间(从1970.1.1开始的毫秒数)
      datetime.getHours();       //获取当前小时数(0-23)
      datetime.getMinutes();     //获取当前分钟数(0-59)
      datetime.getSeconds();     //获取当前秒数(0-59)
      datetime.getMilliseconds();    //获取当前毫秒数(0-999)
      datetime.toLocaleDateString();     //获取当前日期
      this.curdate = datetime.toLocaleTimeString();     //获取当前时间
    },
    onSave: function (mode, item) {
      var self = this;
      console.log("onSave");
      if (mode == 'modeAddNew') {
        console.log("modeAddNew");
        svc.add(item, function (data, status, request) {
          self._find('', 1, 6);
        }, function (data, status, request) {
        });
        this.curitem = {};
        this.setMode('modeList');
      }
      else {
        console.log("edit");
        svc.edit(item, function (item, status, request) {
          alert("修改成功！")
        });
        this.curitem = {};
        self.setMode('modeList');
      }
    }
    ,
    setMode: function (newmode) {
      if (this.mode != newmode) {
        this.mode = newmode;
        switch (newmode) {
          case "modeList":
            this.modeTitle = " [列表]";
            this.curitem.id = null;
            break;
          case "modeItemView":
            this.modeTitle = " [详情]";
            this.currentView = "itemdetails";
            break;
          case "modeEdit":
            this.modeTitle = " [编辑]";
            this.currentView = "itemeditor";
            break;
          case "modeAddNew":
            this.modeTitle = " [新增]";
            this.currentView = "itemadd";
            break;
          default:
            this.modeTitle = "";
        }
      }
    }
  },
  ready: function () {
    this._find('', 1, 6);
  }
});
setTimeout(function (){
  vm.init();
}, 1);
